var util = require("../../../utils/util.js"),
api = require("../../../utils/api.js"), 
app = getApp();
Page({
    data: {
        pageIndex: 1,
        pageSize: 10,
        tabList: [{
            id: 1,
            title: "会员卡"
        }, {
            id: 2,
            title: "VIP约见次卡"
        }],
        dataList: [],
        active: 0,
        playStatusNum:0
    },
    onLoad: function(t) {   
      let _this = this; 
      if (t.active) { 
        _this.setData({
          active: parseInt(t.active),
          dataList: [],
          showEmpty: false
        });
      }
      wx.hideShareMenu()
      _this.getAppointRecords();
    },
    onShow: function() {},
    tabChange: function(t) {
      console.log(t)
        this.setData({
            active: t.detail.index,
            dataList: [],
            showEmpty: false
        });
        this.getAppointRecords();
    },
    async getAppointRecords() { 
        let parms={
            order_type:this.data.tabList[this.data.active].id,
            page: this.data.pageIndex,
            pageSize: this.data.pageSize,
        }
        let {data}=await api.default.myInclaimantList(parms);
        let arr= Array.isArray(data.list)?data.list:[];
        let now=Date.now();
        arr.forEach(item=>{
          item.start_time=util.formatTime(new Date(item.start_time*1000))
          item.end_time=util.formatTime(new Date(item.end_time*1000))
        //   item.create_time=util.formatTime(new Date(item.create_time*1000))
          item.play_time=item.play_time?util.formatTime(new Date(item.play_time*1000)):'';
          item.expire=now>item.end_time*1000?true:false;
        })
        this.setData({
            dataList: arr,
            showEmpty: 0 == arr.length
        });
    },
    // toDetail: function(t) {
    //     wx.navigateTo({
    //         url: "/orderdetail/orderdetail?id=" + t.currentTarget.dataset.id
    //     });
    // },

    toDetail(e) { 
        let id = e.currentTarget.dataset.id; 
        let ordernum = e.currentTarget.dataset.ordernum; 
        wx.navigateTo({
          url: '/pages/mine/myorder/orderdetail?id=' + id+"&ordernum="+ordernum, 
        })
      },


    telphone(e){
      api.default.config().then(res => {
        const {business_service_mobile}=res.data;
        if(!business_service_mobile)return wx.showToast({
          title: '客服电话暂未配置',
          icon: 'none'
        })
        wx.makePhoneCall({
          phoneNumber: business_service_mobile
        })
      })
    },
    toPay(e) {
      let self = this;
      wx.showLoading({
        title: '支付中..',
      }) 
      var order_id = e.currentTarget.dataset.id;
      const parms = {
        order_id: order_id 
      }
      api.default.purchaseCard(parms).then(res => {
        wx.hideLoading();
        if (res.code == 1) {
          wx.requestPayment({
            timeStamp: res.data.timeStamp,  //后端返回的时间戳
            nonceStr:  res.data.nonceStr,   //后端返回的随机字符串
            package:  res.data.package, //后端返回的prepay_id
            signType: 'MD5', //后端签名算法,根据后端来,后端MD5这里即为MD5
            paySign:  res.data.paySign,  //后端返回的签名
            success(result) {
              wx.showLoading({
                title: '支付中',
              })
              self.setData({playStatusNum:1})
              self.timers = setInterval(() => self.getOrderStatus({
                order_num: res.order_num
              }), 800)
            },
            fail(res) {
              wx.showToast({
                title: '支付失败',
                icon:'none'
              })
            }
          })
        }
      })
    },
    cancel(e) { 
      let self = this; 
      wx.showLoading();
      var order_id = e.currentTarget.dataset.id;
      const parms = {
        order_id: order_id 
      }
      api.default.cancelOrder(parms).then(res => {
        wx.hideLoading();
        if (res.code == 1) {
            wx.showToast({
              title: '取消成功',
              success: function() { 
              }
            })
            self.getAppointRecords();
        }
      })
    },
    getOrderStatus(parms) {
      api.default.getOrderStatus(parms).then(res => {
        if (res.data.status == 1) {
          wx.hideLoading();
          clearInterval(this.timers);
          wx.showModal({
            title: '提示',
            content: '您的订单已支付成功',
            showCancel: false,
            confirmText: '知道了',
            complete: (res) => {
              if (res.confirm) {
                this.getAppointRecords();
              }
            }
          })
        }else{
            let num=this.data.playStatusNum;
            if(num > 10){
              clearInterval(this.timers);
              return wx.showToast({
                icon:'none',
                title: '支付失败',
              })
            }
            num++;
            this.setData({playStatusNum:num})
        }
      })
    },
    onReachBottom: function() {
        if(this.data.isScroll){
            let page=Number(this.data.pageIndex)+1;
            this.setData({isScroll:false,pageIndex:page})
            this.getAppointRecords()
        }
    }
});